import { fileURLToPath, URL } from 'node:url'
import { defineConfig, loadEnv, } from 'vite'
import type { ConfigEnv } from 'vite'
import vue from '@vitejs/plugin-vue'
import vueJsx from '@vitejs/plugin-vue-jsx'
import Components from 'unplugin-vue-components/vite'
import vueSetupExtend from 'vite-plugin-vue-setup-extend'

import {
  // AntDesignVueResolver,
  ElementPlusResolver,
  // VantResolver,
} from 'unplugin-vue-components/resolvers'

// 各部分打包体积的可视化分析
import { visualizer } from 'rollup-plugin-visualizer'

// 配置项文档：https://vitejs.dev/config/
export default defineConfig(({ command, mode }: ConfigEnv) => {

  const env = loadEnv(mode, process.cwd(), '')


  return {
    define: {
      'process.env': env
    },
    plugins: [
      vue(),
      vueJsx(),
      vueSetupExtend(),
      Components({
        resolvers: [
          ElementPlusResolver({
            importStyle: "sass",
            directives: true,
            version: "2.1.5",
          }),
        ],
        dts: true,
      }),
      visualizer({
        emitFile: true,
        filename: "buildAnalysis.html",
        open: true,
        gzipSize: true,
        brotliSize: true,
      }),
    ],
    build: {
      assetsDir: 'static',
      chunkSizeWarningLimit: 2000,
      minify: 'terser',
      terserOptions: {
        compress: {
          drop_console: false,
          drop_debugger: true,
        },
        format: {
          comments: false
        }
      },
    },
    css: {
      preprocessorOptions: {
        scss: {
          additionalData: `@use "@/theme/elPlus.theme.scss" as *;`,
        },
      }
    },
    resolve: {
      alias: {
        '@': fileURLToPath(new URL('./src', import.meta.url)),
        'vue-i18n': 'vue-i18n/dist/vue-i18n.cjs.js',
      }
    },
    server: {
      port: 13142,
      open: true,
      host: true,
      proxy: {
        "/dev/api": {
          rewrite: (path) => path.replace(/^\/dev\/api/, ''),
          target: "http://localhost:5000",
          changeOrigin: true,
          ws: true,
        },
        "/dev/s5001": {
          rewrite: (path) => path.replace(/^\/dev\/s5001/, ''),
          target: "http://localhost:5001",
          changeOrigin: true,
          ws: true,
        },
      }
    }
  }
})
